package gui.ScheduleTab;
import gui.Interpreter;
import java.util.List;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import common.CommandAnswer;
import common.MyDate;
import common.Tables.Table;

/**
 * this class open in separate thread 
 * 			the save schedule command to interpreter  
 * @author Orit
 *
 */
public class SaveSchedule implements Runnable{
	Interpreter interpreter;		//interpreter each command to send to server
	public boolean done = false;	//notice if the thread done (to keep drawing the GUI)
	private Display display;		//DISPLAY GUI
	private Shell shell;			//shell GUI
	CommandAnswer ans;				//the answer for save schedule
	MyDate to;						// the date of first table
	MyDate from;					// the date of second table
	org.eclipse.swt.widgets.Table todayTable;		//the first table
	org.eclipse.swt.widgets.Table tomorrowTable;	// the second table
	org.eclipse.swt.widgets.Table tree;				//include unSchedle missions
	
	/**
	 * C'tor
	 * @param interpreter- to send the command
	 * @param display - of the GUI	
	 * @param shell		- Of the GUI
	 * @param from - the date of first table
	 * @param to - the date of second table
	 * @param todayTable	- the first table
	 * @param tomorrowTable - the second table
	 * @param tree - include unSchedle missions
	 */
	public SaveSchedule(Interpreter interpreter, Display display,
			Shell shell, MyDate from, MyDate to,
			org.eclipse.swt.widgets.Table todayTable,
			org.eclipse.swt.widgets.Table tomorrowTable,
			org.eclipse.swt.widgets.Table tree) {
		this.interpreter = interpreter;
		this.display = display;
		this.shell = shell;
		this.to = to;
		this.from = from;
		this.todayTable = todayTable;
		this.tomorrowTable = tomorrowTable;
		this.tree = tree;
	}
	
	/**
	 * run the save schedule command in separate thread and continue drawing
	 */
	public void run(){
		done = false;
		 Thread thread = new Thread(new Runnable() { //open in separate thread
			@Override
			public void run() {
				saveschedule();
				done = true;
				display.wake();
			}
		 });
		 thread.start();
        while (!done && !shell.isDisposed()) {   //Continue to draw the main GUI. (no black screen)
          if (!display.readAndDispatch())
            display.sleep();
        }
		
	}
	
	/**
	 * the save command that send to server
	 */
	private void saveschedule() {
		// TODO Auto-generated method stub
		interpreter.saveSchedule(todayTable,tomorrowTable,tree,from.getDateFormat(),to.getDateFormat());
	}
	
	/**
	 * get First Table in Gui (dateTable1)
	 * @return first table of save Scheduling answer
	 */
	public List<Table> getFirstTable() {
		return ans.getListTableAns();
	}
	
	/**
	 * get Second Table in Gui (dateTable1)
	 * @return Second table of sacw Scheduling answer
	 */
	public List<Table> getSecTable() {
		return ans.getListTableSecAns();
	}
	
	/**
	 * is thread was done
	 * @return true if the thread finish false otherwise
	 */
	public boolean isDone() {
		return done;
	}
}
